<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

function dayMonthYearSelectBox($intTime, $dayID='day', $monthID= 'month', $yearID= 'year', $arrayReturn = array('d','m','y'), $extra){
	if($intTime == 0){
		$date = $month = $year = 0;
	}else{
		$date = date('d', $intTime);
		$month = date ('F', $intTime);
		$year = date ('Y', $intTime);
	}
	 
	$d = "<select id='$dayID' name='$dayID' $extra>";
	for($i = 1; $i<=31 ;$i++){
		$today = date("d", mktime(0, 0, 0, 0, $i));
		$selected = ($date == $today) ? "selected = 'selected'":'';
		$d .= "<option $selected value='$i'>$today</option>";
	}
	$d .="</select>";
	
	$m = "<select id='$monthID' name='$monthID' $extra>"; 
	for($i = 1; $i<=12 ;$i++){
		$month2 = date("F", mktime(0, 0, 0, $i, 1));
		$selected = ($month == $month2) ? "selected = 'selected'":'';
		$m .= "<option $selected value='$i'>$month2</option>";
	}
	$m .="</select>";
	
	$y = "<select id='$yearID' name='$yearID' $extra>"; 
	$yearNow = date('Y',time());
	for($i = 2010; $i<=$yearNow+10 ;$i++){
		$selected = ($year == $i) ? "selected = 'selected'":'';
		$y .= "<option $selected value='$i'>$i</option>";
	}
	$y .="</select>";
	
	$arrayUI = array();
	foreach($arrayReturn as $val){
		if(in_array($val, array('d','m','y'))){
			$arrayUI[] = ${$val};
		}
	}
	return implode('',$arrayUI);
}

function convertTimeEntity2Int($y,$m,$d){
	return mktime(0, 0, 0, $m, $d, $y);
}

function convertToTimeStampFromFormat( $format = "d-F-Y", $str){
	if(ENVIRONMENT == "local"){
		$date = DateTime::createFromFormat($format, $str);
		if(!$date) return 0;
		$date = $date->format('Y-m-d');
		return intval( strtotime($date) );
	}else{
		//use in UNIX and php < 5.3  (  :(  )
		$month_array = array("January"=>1, "February"=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6,
							"July"=>7, "August"=>8, "September"=>9, "October"=>10, "November"=>11,"December"=>12 );
		$parse = explode('-', $str);
		 
		if(empty($parse) || !is_numeric($parse[0]) || !is_numeric($parse[2])){
			return 0;
		}
		$timestamp = mktime(0, 0, 0, $month_array[$parse[1]], $parse[0], $parse[2]);
		return $timestamp; 
	}
}

function sbxDateFormat($timeStamp, $format = "d-F-Y"){
	$format = isset($format) ? $format : 'd-F-Y';
	return date($format, $timeStamp);
}

function workingDaysTimeStamp($workDays = 2){
	$timestamp = time();
    while ($workDays>0) {
        $timestamp += 86400;
        if (date('N', $timestamp)<6) $workDays--;
    }
    return $timestamp;
}

function workingDaysTimeStamp2($workDays = 2, $fromTimeStamp=0){
	$timestamp = $fromTimeStamp;
    while ($workDays>0) {
        $timestamp += 86400;
        if (date('N', $timestamp)<6) $workDays--;
    }
    return $timestamp;
}

